<template>
  <div class="app-container">
    <el-descriptions class="margin-top" title="长者信息" :column="4" size="medium" border>
      <template slot="extra">
        <!-- 搜索 -->
        <el-radio-group v-model="radio4" size="mini" class="filter-item">
          <el-radio-button label="ReadCardBas" name="医保卡">医保卡</el-radio-button>
          <el-radio-button label="ReadSFZ" name="身份证">身份证</el-radio-button>
        </el-radio-group>
        <el-button v-loading.fullscreen.lock="fullscreenLoading" class="filter-item" size="small" type="primary" icon="el-icon-bank-card" @click="read">读 卡</el-button>
        <el-button class="filter-item" size="small" type="warning" icon="el-icon-remove-outline" @click="doOpen()">终止待遇</el-button>
      </template>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="user" />
          姓名
        </template>
        {{ this.person_xm }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="xingbie" />
          性别
        </template>
        {{ this.person_xb }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="shenfenzheng" />
          身份证
        </template>
        {{ this.person_sfz }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="date" />
          出生日期
        </template>
        {{ this.person_csrq }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="datestart" />
          待遇开始日期
        </template>
        {{ this.bis_zhsq_dykkrq }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="dateend" />
          待遇结束日期
        </template>
        {{ this.bis_zhsq_dyjsrq }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="jiekuanqixian" />
          享受待遇期
        </template>
        {{ this.bis_zhsq_xsdyq }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="dengjizhongxin" />
          失能等级
        </template>
        {{ this.bis_zhsq_sndj }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="kehubianhao" />
          个人编号
        </template>
        {{ this.person_grbh }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="qiahao" />
          医保卡号
        </template>
        {{ this.person_ybkh }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="sign" />
          参保类型
        </template>
        {{ this.person_cblx }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="zhuangtai_2" />
          参保状态
        </template>
        {{ this.person_cbzt }}
      </el-descriptions-item>

      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="user1" />
          监护人姓名
        </template>
        {{ this.person_jhrxm }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="dianhua" />
          监护人电话
        </template>
        {{ this.person_jhrdh }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="lishuguanxi" />
          与失能人员关系
        </template>
        {{ this.person_gx }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="quyu" />
          参保统筹区
        </template>
        {{ this.person_ybssqx }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="zhuyuanjiancha" />
          是否在院
        </template>
        <span v-if="this.person_ifzy === '是'"><span style="color: red;">{{ this.person_ifzy }}</span></span>
        <span v-else>{{ this.person_ifzy }}</span>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon icon-class="yanglaofuyou" />
          在院机构
        </template>
        <span v-if="this.person_ifzy === '是'"><span style="color: red;">{{ this.person_szjg }}</span></span>
        <span v-else>{{ this.person_szjg }}</span>
      </el-descriptions-item>
    </el-descriptions>
    <el-dialog :visible.sync="dialogZzdyFormVisible" title="终止待遇" width="500px">
      <el-form>
        <el-form-item label="终止日期" prop="zzrq">
          <el-date-picker v-model="zzrq" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" style="width: 150px;" @change="listening" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogZzdyFormVisible = false">取消</el-button>
        <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="doZzdy()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudZhsq from '@/api/business/zhsq'
import { getZhid } from '@/api/business/zhsq'
import { queryBySfz, queryByGrbm, stopZhsq } from '@/api/business/stopZhsq'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import axios from 'axios'
import moment from 'moment'
import { mapGetters } from 'vuex'

const defaultForm = { bisZhsqId: null, bisZhsqHljgId: null, bisZhsqSnryId: null, bisZhsqSnryXm: null, bisZhsqSnryXb: null, bisZhsqSnrySfz: null, bisZhsqDykkrq: null, bisZhsqYbkh: null, bisZhsqCbzt: null, bisZhsqDyjsrq: null, bisZhsqXsdyq: null, bisZhsqAdl: null, bisZhsqZt: null, bisZhsqSnryYbssqx: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, bz: null, bisZhsqAdlPc: null, bisZhsqSndj: null, bisZhsqRybh: null, bisZhsqNl: null, bisZhsqLxdh: null, bisZhsqHldzSsqx: null, bisZhsqHldzXxdz: null, bisZhsqHldz: null, bisZhsqJhrxm: null, bisZhsqJhrdh: null, bisZhsqGx: null, bisZhsqSqyy: null, bisZhsqGrbh: null, adlZhsqs: [], bisZhsqCblx: null }
export default {
  name: 'Zzdy',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['fwcsxz', 'jglx', 'ccs', 'ccstcqh', 'yllb', 'yjsgx'],
  cruds() {
    return CRUD({ title: '照护申请', url: 'api/zhsq', idField: 'bisZhsqId', sort: 'bisZhsqId,desc', crudMethod: { ...crudZhsq }})
  },
  data() {
    return {
      zzrq: '',
      dialogZzdyFormVisible: false,
      person_zhid: '',
      person_lxdh: '',
      person_jhrxm: '',
      person_jhrdh: '',
      person_gx: '',
      person_xm: '',
      person_sfz: '',
      person_xb: '',
      person_nl: '',
      person_csrq: '',
      person_tx: '',
      person_grbh: '',
      person_ybkh: '',
      person_ybssqx: '',
      person_cblx: '',
      person_cbzt: '',
      bis_zhsq_sndj: '',
      bis_zhsq_dykkrq: '',
      bis_zhsq_dyjsrq: '',
      bis_zhsq_xsdyq: '',
      person_ifzy: '',
      person_szjg: '',
      fullscreenLoading: false,
      radio4: 'ReadCardBas'
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    listening() {
      if (!(moment(this.zzrq).isAfter(this.bis_zhsq_dykkrq) && moment(this.zzrq).isBefore(this.bis_zhsq_dyjsrq))) {
        this.zzrq = ''
        this.$notify({
          title: '终止日期必须选择在' + this.bis_zhsq_dykkrq + '~' + this.bis_zhsq_dyjsrq + '之间',
          type: 'warning',
          duration: 2500
        })
        return
      }
    },
    // 获取弹窗内评估公司数据
    read() {
      const that = this
      this.fullscreenLoading = true
      axios.get('http://127.0.0.1:19995?MSG=' + this.radio4).then(res => {
        console.log('res: {} ', res)
        if (this.radio4 === 'ReadCardBas') {
          const shbzkContent = res.data.pCardInfo
          if (shbzkContent === '') {
            this.$notify({
              title: '重新读卡',
              type: 'warning',
              duration: 2500
            })
            return
          } else {
            const contentList = shbzkContent.split('|')
            this.person_sfz = contentList[1]
          }
        } else {
          const pOutBuff = res.data.pOutBuff
          if (pOutBuff === '证件信息失败') {
            this.$notify({
              title: '请将身份证放入正确位置',
              type: 'error',
              duration: 2500
            })
          } else {
            this.person_sfz = pOutBuff.split('|')[0]
          }
        }
        that.person_sfz = that.user.username === '6002' ? '220104200110263322' : that.person_sfz
        console.log('that.person_sfz: {} ', that.person_sfz)
        this.freshZhxx()
      }).catch(err => {
        this.fullscreenLoading = false
        console.log('err: {} ', err)
      })
    },
    freshZhxx() {
      queryBySfz({ sfz: this.person_sfz }).then(res1 => {
        console.log('getZhid:', Object.keys(res1).length)
        if (Object.keys(res1).length !== 0) {
          this.person_sfz = res1.bisZhsqSnrySfz
          this.person_xm = res1.bisZhsqSnryXm
          this.person_xb = res1.bisZhsqSnryXb
          this.person_nl = res1.bisZhsqNl
          this.person_zhid = res1.bisZhsqId
          this.person_lxdh = res1.bisZhsqLxdh
          this.person_jhrdh = res1.bisZhsqJhrdh
          this.person_jhrxm = res1.bisZhsqJhrxm
          this.person_grbh = res1.bisZhsqGrbh
          this.person_ybkh = res1.bisZhsqYbkh
          this.person_ybssqx = this.dict.label.ccstcqh[res1.bisZhsqSnryYbssqx]
          this.person_gx = this.dict.label.yjsgx[res1.bisZhsqGx]
          this.person_cblx = res1.bisZhsqCblx
          this.person_cbzt = res1.bisZhsqCbzt
          this.bis_zhsq_sndj = res1.bisZhsqSndj
          this.bis_zhsq_dykkrq = res1.bisZhsqDykkrq.substring(0, 10)
          this.bis_zhsq_dyjsrq = res1.bisZhsqDyjsrq.substring(0, 10)
          this.bis_zhsq_xsdyq = res1.bisZhsqXsdyq
          const bornDay = this.person_sfz.substring(6, 14)
          this.person_csrq = bornDay.substring(0, 4) + '年' + bornDay.substring(4, 6) + '月' + bornDay.substring(6, 8) + '日'
          queryByGrbm({ grbm: this.person_grbh }).then(res2 => {
            this.fullscreenLoading = false
            console.log('res2:', res2)
            this.person_ifzy = res2.akc318Mc === '' ? '否' : '是'
            this.person_szjg = res2.akc318Mc
          })
        } else {
          this.$notify({
            title: '长者没有复核通过的照护待遇',
            type: 'warning',
            duration: 2500
          })
        }
        this.fullscreenLoading = false
      })
    },
    doOpen() {
      if (this.person_xm === '') {
        this.$notify({
          title: '请先读卡',
          type: 'warning',
          duration: 2500
        })
        return
      }
      if (this.person_ifzy === '是') {
        this.$notify({
          title: this.person_xm + '为在院状态，请先结算或无费出院，再终止待遇',
          type: 'warning',
          duration: 2500
        })
        return
      }
      this.dialogZzdyFormVisible = true
    },
    doZzdy() {
      if (this.zzrq === undefined || this.zzrq === '') {
        this.$notify({
          title: '请填写终止日期',
          type: 'warning',
          duration: 2500
        })
        return
      }
      this.$confirm('你确定终止待遇吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.fullscreenLoading = true
        stopZhsq({ sfz: this.person_sfz, zzrq: this.zzrq, zhid: this.person_zhid })
          .then(res => {
            this.fullscreenLoading = false
            this.dialogZzdyFormVisible = false
            this.$notify({
              title: '待遇已终止',
              type: 'success',
              duration: 2500
            })
          })
          .catch(err => {
            this.fullscreenLoading = false
            this.dialogZzdyFormVisible = false
            console.log(err)
          })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
}
.user-info {
  padding-left: 0;
  list-style: none;
  li{
    border-bottom: 1px solid #F0F3F4;
    padding: 11px 0;
    font-size: 13px;
  }
  .user-right {
    float: right;
    a{
      color: #317EF3;
    }
  }
}
</style>

